package com.tdam2013.grupo08.data;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.tdam2013.grupo08.helpers.ContactoHelper;
import com.tdam2013.grupo08.models.Accion;
import com.tdam2013.grupo08.models.Contacto;

public class LlamadaEntranteReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		ContentResolver cr=context.getContentResolver();
		DataBaseHelper db = new DataBaseHelper(context);
		Bundle extra = intent.getExtras();
		if (extra != null) {
			String state = extra.getString(TelephonyManager.EXTRA_STATE);
			if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
				
				String numero=extra.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
				
				ContactoHelper ch=new ContactoHelper();
				Contacto c=ch.getContacto(context, numero);
				if(c.getId()!=null)
					db.registrarAccionLocal(Accion.TIPO_LLAMADA_ENTRANTE, c.getId(), null);
				else				
					db.registrarAccionLocal(Accion.TIPO_LLAMADA_ENTRANTE, numero, null);
				
//				Log.i("LLAMADA", "Llamada entrante"+numero+"----"+i);
			}
		}
	}

}